<template>
  <el-card style="margin:20px">
    <el-tabs v-model="activeName">
      <el-tab-pane label="登录账户设置" name="loginInfo">
        <el-form
          ref="baseForm"
          :model="userInfo"
          :rules="rules"
          label-width="80px"
          style="width:350px;margin:50px auto;"
        >
          <el-form-item label="姓名：" prop="username">
            <el-input v-model="userInfo.username"></el-input>
          </el-form-item>
          <el-form-item label="密码：" prop="password">
            <el-input type="password" v-model="userInfo.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="checkForm">更新</el-button>
            <el-button @click="$router.back()">取消</el-button>
          </el-form-item>
        </el-form>
      </el-tab-pane>
      <el-tab-pane label="个人详情" name="userInfo">
        <userInfo></userInfo>
      </el-tab-pane>
      <el-tab-pane label="岗位信息" name="jobInfo">
        <jobInfo></jobInfo>
      </el-tab-pane>
    </el-tabs>
  </el-card>
</template>

<script>
// 导入员工基本信息
import { userBaseInfo } from '@/api/user'
import { saveBaseInfo } from '@/api/emloyees'
// 导入个人详情和岗位信息
import jobInfo from './components/job-info.vue'
import userInfo from './components/user-info.vue'
export default {
  components: {
    jobInfo,
    userInfo
  },
  name: 'detail',
  data () {
    return {
      activeName: 'loginInfo',
      userInfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入您的姓名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入您的密码', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 获取员工基本资料
    async getBaseInfo () {
      const res = await userBaseInfo(this.$route.query.id)
      //   console.log(res)
      this.userInfo = res.data
    },
    // 校验发起请求，保存员工基本信息
    checkForm () {
      this.$refs.baseForm.validate(async v => {
        if (v) {
          // 发起请求
          const res = await saveBaseInfo(this.userInfo)
          console.log('修改后', res)
          // 提示成功
          this.$message.success('修改成功')
        }
      })
    }
  },
  created () {
    this.getBaseInfo()
  }
}
</script>

<style></style>
